package com.sky.controller.user;

import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/user")
@Api(tags = "用户端菜品浏览接口")
@RestController
public class UserDishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @ApiOperation("分类菜品查询")
    @GetMapping("/dish/list")
    public Result<List<DishVO>> UserDishSearch(Integer categoryId) {
        String key = "dish_" + categoryId;
        //判断是否有缓存redis string
        List<DishVO> List = (List<DishVO>) redisTemplate.opsForValue().get(key);
        if (List != null) {
            return Result.success(List);
        }
        List<DishVO> dishList = dishService.UserDishSearch(categoryId);
        log.info("{}", dishList);
        redisTemplate.opsForValue().set(key, dishList);
        return Result.success(dishList);
    }
}
